package cn.demomaster.quickpicturer.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager2.widget.ViewPager2;

import java.util.ArrayList;

import cn.demomaster.quickpicturer.R;
import cn.demomaster.quickpicturer.adapter.ImageFragmentAdapter;
import cn.demomaster.quickpicturer.model.ImageInfo;

/**
 *
 */
public class PreviewActivity extends AppCompatActivity {
    //private PhotoView pv_image;
    private ImageView iv_back;
    private TextView tv_title;
    private ViewPager2 vp_image;
    private ImageFragmentAdapter fragmentAdapter;

    public static void startActivity(Context context, ArrayList<ImageInfo> imageInfos,int imageIndex) {
        Intent intent = new Intent(context, PreviewActivity.class);
        intent.putExtra("imageIndex", imageIndex);
        intent.putParcelableArrayListExtra("imagesInfos", imageInfos);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_picture_preview);
        // 针对非AppCompat活动
        if (getSupportActionBar() != null) {
            getSupportActionBar().hide();
        }
        //getActionBarTool().setActionBarType(ACTION_STACK_NO_STATUS);
        //getActionBarTool().setStateBarColorAuto(true);
        vp_image = findViewById(R.id.viewpager_image_preview);
        //pv_image = (PhotoView) findViewById(R.id.pv_image);
        initV();
    }

    private void initV() {
        tv_title = findViewById(R.id.tv_title);
        iv_back = findViewById(R.id.iv_back);
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        //pv_image = findViewById(R.id.pv_image);
        Bundle mBundle = getIntent().getExtras();
        if (mBundle != null ) {
            int imageIndex = getIntent().getIntExtra("imageIndex", 0);
            imageInfos = getIntent().getParcelableArrayListExtra("imagesInfos");
            initViewPager(imageIndex);
//            if(mBundle.containsKey("imageFiles")) {
//                List<ImageInfo> imagePaths = (ArrayList<Image>) mBundle.getSerializable("imageFiles");
//                images = new ArrayList<>();
//                for(Image image:imagePaths){
//                    images.add(image);
//                }
//                index = mBundle.getInt("imageIndex", 0);
//                initViewPager(index);
//            }
//            if(mBundle.containsKey("images")) {
//                images = (ArrayList<Image>) mBundle.getSerializable("images");
//                index = mBundle.getInt("imageIndex", 0);
//                initViewPager(index);
//            }
            //File file = new File(image.getPath());
            /*FileInputStream fs = null;
            try {
                fs = new FileInputStream(image.getPath());
                Bitmap bitmap = BitmapFactory.decodeStream(fs);
                Uri imageUri = Uri.parse(MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, null, null));
                photo_view.setImageURI(imageUri);
                actionBarLayout.setStateBarColorAuto(true);
            } catch (FileNotFoundException e) {
            QdLog.e(e);
            }*/
        }
    }

    private ArrayList<ImageInfo> imageInfos;
    ViewPager2.OnPageChangeCallback onPageChangeCallback = new ViewPager2.OnPageChangeCallback() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            super.onPageScrolled(position, positionOffset, positionOffsetPixels);
        }

        @Override
        public void onPageSelected(int position) {
            super.onPageSelected(position);
            if(imageInfos!=null) {
                setTitle(((position + 1) + "") + ("/" + imageInfos.size()));
                tv_title.setText(((position + 1) + "") + ("/" + imageInfos.size()));
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {
            super.onPageScrollStateChanged(state);
        }
    };
    public void initViewPager(final int index) {
        if (imageInfos == null || imageInfos.size() == 0) {
            return;
        }
        setTitle((index + 1) + "/" + imageInfos.size());
        fragmentAdapter = new ImageFragmentAdapter(this, imageInfos);
        vp_image.unregisterOnPageChangeCallback(onPageChangeCallback);
        vp_image.registerOnPageChangeCallback(onPageChangeCallback);
        vp_image.setAdapter(fragmentAdapter);
        vp_image.setCurrentItem(index,false);
    }
}
